package com.xuxin.baby.activity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.EncryptUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.xuxin.baby.R;
import com.xuxin.baby.base.BaseActivity;
import com.xuxin.baby.base.BaseConstant;
import com.xuxin.baby.bean.BaseBean;
import com.xuxin.baby.bean.UserDataBean;
import com.xuxin.baby.contract.UserContract;
import com.xuxin.baby.presenter.UserPresenter;
import com.xuxin.baby.utils.ACache;
import com.xuxin.baby.view.CountDown;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xu_xin
 * Created on 2020/12/2 0002
 * 修改手机号
 */
public class SettingPhoneActivity extends BaseActivity implements UserContract.View {
    private final UserContract.Presenter presenter = new UserPresenter(this);
    @BindView(R.id.title_left)
    ImageView title_left;
    @BindView(R.id.title_right)
    ImageView title_right;
    @BindView(R.id.title_name)
    TextView title_name;
    @BindView(R.id.button)
    Button button;

    @BindView(R.id.setting_phone)
    EditText setting_phone;
    @BindView(R.id.setting_phone_code)
    EditText setting_phone_code;
    @BindView(R.id.setting_phone_send)
    CountDown setting_phone_send;

    private ACache aCache;
    private String tenantId;//租户id
    private String token;//授权token
    private String memberId;//用户id
    private String phone;//手机号

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_setting_phone);
    }

    @Override
    public void initView() {
        title_name.setText("修改手机号码");
        button.setText("保存");
    }

    @Override
    public void initData() {
        aCache = ACache.get(context);
        tenantId = aCache.getAsString(BaseConstant.tenantId);
        token = aCache.getAsString(BaseConstant.token);
        memberId = aCache.getAsString(BaseConstant.memberId);
        handler.sendEmptyMessage(0);
    }

    @Override
    public void onComplete() {

    }

    @Override
    @OnClick({R.id.title_left, R.id.setting_phone_send, R.id.button})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.title_left) {
            finish();
        } else if (id == R.id.setting_phone_send) {
            phone = setting_phone.getText().toString().trim();
            if (!RegexUtils.isMobileExact(phone)) {
                ToastUtils.showShort("请输入正确的手机号码");
            } else if (!setting_phone_send.startTimer()) {
                ToastUtils.showShort("时间还没到呢");
            } else {
                Message message = Message.obtain();
                message.what = 1;
                message.obj = getCodeMessage(phone);
                handler.sendMessage(message);
            }
        } else if (id == R.id.button) {
            phone = setting_phone.getText().toString().trim();
            String inputCode = setting_phone_code.getText().toString();
            String updatePhone = aCache.getAsString("updatePhone");
            String updateCode = aCache.getAsString("updateCode");
            LogUtils.e("phone = " + phone);
            LogUtils.e("updateCode = " + updateCode);
            LogUtils.e("inputCode = " + inputCode);
            if (!RegexUtils.isMobileExact(phone) || !phone.equals(updatePhone)) {
                ToastUtils.showShort("请输入正确的手机号码");
            } else if (StringUtils.isEmpty(updateCode)) {
                ToastUtils.showShort("验证码已过期");
            } else if (!inputCode.equals(updateCode)) {
                ToastUtils.showShort("动态密码错误");
            } else {
                handler.sendEmptyMessage(2);
            }
        }
    }

    private final Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case 0:
                    presenter.getUserData(tenantId, token, memberId);
                    break;
                case 1://发送验证码
                    //presenter.sendCode(phone, code);
                    setting_phone_code.setText((String) msg.obj);
                    break;
                case 2:
                    presenter.updateUserData(tenantId, token, memberId,
                            phone,
                            userDataBean.getResult().getAvatar(),
                            userDataBean.getResult().getNickname(),
                            userDataBean.getResult().getBirthday(),
                            userDataBean.getResult().getEthnicity(),
                            userDataBean.getResult().getRole(),
                            userDataBean.getResult().getBirthAddress(),
                            userDataBean.getResult().getAddress());
                    break;
            }
            return false;
        }
    });

    private UserDataBean userDataBean;

    @Override
    public void onSuccess(UserDataBean userDataBean) {
        this.userDataBean = userDataBean;

    }

    @Override
    public void onImageSuccess(BaseBean baseBean) {

    }

    @Override
    public void onSuccess(BaseBean baseBean) {
        setResult(200);
        finish();
    }

    /**
     * 验证码
     */
    private String getCodeMessage(String phone) {
        String time = phone + System.currentTimeMillis();
        String md5 = EncryptUtils.encryptMD5ToString(time);
        String updateCode = md5.substring(md5.length() - 6);
        aCache.put("updatePhone", phone);
        aCache.put("updateCode", updateCode, ACache.time_minutes);
        LogUtils.e("getCodeMessage = " + updateCode);
        return updateCode;
    }
}